package com.feonix.common.util;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;

import com.feonix.common.annotation.ColumnType;
import com.feonix.common.basedao.LikeType;

public class SqlUtil {

	private static String[] regax1 = { "%", "_", "[", "]", "^", "\\" };

	private static String[] regax2 = { "\\%", "\\_", "\\[", "\\]", "\\^", "\\\\" };

	/**
	 * 转义sql中的通配符，建议不要对整个sql转，只对字段的值转义
	 * @param value
	 * @param likeType
	 * @return
	 */
	public static String getLikeValue(String value, LikeType likeType) {
		if (value != null) {
			value = escapeLikeValue(value);
			switch (likeType.ordinal()) {
			case 1:
				value = "%" + value;
				break;
			case 2:
				value = value + "%";
				break;
			case 0:
				value = "%" + value + "%";
			}
		}

		return value;
	}

	/**
	 * 转义sql中的通配符，建议不要对整个sql转，只对字段的值转义
	 * @param value
	 * @return
	 */
	public static String escapeLikeValue(String value) {
		if (value != null) {
			value = StringUtils.replaceEach(value, regax1, regax2);
		}

		return value;
	}

	/**
	 * 转义sql中的特殊字符
	 * @param string
	 * @return
	 */
	public static String escapeSql(String sql) {
		return StringEscapeUtils.escapeSql(sql);
	}

	/**
	 * 判断sql中是否有 '
	 * @param sql
	 * @return
	 */
	public static boolean isSafeSql(String sql) {
		boolean isSafe = true;
		String str = StringUtil.getString(sql).toLowerCase().trim();
		if (str.startsWith("insert") && StringUtil.contains(sql, "'")) {
			isSafe = false;
		}
		return isSafe;
	}

	
	/**
	 * 清理where条件头尾空格及AND、OR
	 * @param sqlWhere
	 * @return
	 */
	public static String trimWhere(String sqlWhere) {
		return trimWhere(sqlWhere, true);
	}

	/**
	 * 清理where条件头尾空格及AND、OR
	 * @param sqlWhere
	 * @param paramBoolean
	 * @return
	 */
	public static String trimWhere(String sqlWhere, boolean paramBoolean) {
		sqlWhere = sqlWhere.trim();

		String str = sqlWhere.toLowerCase();

		if (str.startsWith("and "))
			sqlWhere = sqlWhere.substring(4);
		else if (str.startsWith("or ")) {
			sqlWhere = sqlWhere.substring(3);
		}

		str = sqlWhere.toLowerCase();

		if (str.endsWith(" and"))
			sqlWhere = sqlWhere.substring(0, sqlWhere.length() - 4);
		else if (str.endsWith(" or")) {
			sqlWhere = sqlWhere.substring(0, sqlWhere.length() - 3);
		}

		if ((paramBoolean) && (StringUtil.getStringTrim(sqlWhere).length() > 0)) {
			sqlWhere = " WHERE " + sqlWhere;
		}

		return sqlWhere;
	}

	/**
	 * 清理where条件头尾空格及AND、OR
	 * @param sqlWhere
	 * @return
	 */
	public static String trimWhere(StringBuilder sqlWhere) {
		return trimWhere(sqlWhere, true);
	}

	/**
	 * 清理where条件头尾空格及AND、OR
	 * @param sqlWhere
	 * @param paramBoolean
	 * @return
	 */
	public static String trimWhere(StringBuilder sqlWhere, boolean paramBoolean) {
		return trimWhere(sqlWhere.toString(), paramBoolean);
	}

	/**
	 * 清理where条件头尾空格及AND、OR
	 * @param sqlWhere
	 * @return
	 */
	public static String trimWhere(StringBuffer sqlWhere) {
		return trimWhere(sqlWhere, true);
	}

	/**
	 * 清理where条件头尾空格及AND、OR
	 * @param sqlWhere
	 * @param paramBoolean
	 * @return
	 */
	public static String trimWhere(StringBuffer sqlWhere, boolean paramBoolean) {
		return trimWhere(sqlWhere.toString(), paramBoolean);
	}

	/**
	 * 获得默认字段长度
	 * @param paramColumnType
	 * @param paramInt
	 * @return
	 */
	public static int[] resolvingLength(ColumnType columnType, int decimalLenght) {
		int i = 0;
		switch (columnType.ordinal()) {
		case 2:
			i = 11;
			if (decimalLenght == -1) {
				decimalLenght = 0;
			}
			break;
		case 4:
			i = 20;
			if (decimalLenght == -1) {
				decimalLenght = 1;
			}
			break;
		case 3:
			i = 20;
			if (decimalLenght == -1) {
				decimalLenght = 2;
			}
			break;
		case 0:
			i = 20;
			break;
		case 5:
			i = 255;
			break;
		case 6:
			i = 50;
			break;
		case 1:
			
		default:
			i = 0;
		}

		int[] arrayOfInt = new int[2];
		arrayOfInt[0] = i;
		arrayOfInt[1] = decimalLenght;
		return arrayOfInt;
	}

}
